package com.orzxlee.FunctionInterface06;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * 第四节 函数接口
 *
 *  常用函数式接口 Consumer 消费型接口
 *
 *  Consumer接口练习
 *
        String [] strArray = {"林青霞,30","张曼玉,35","王祖贤,33"};
 *      按照"姓名:xx,年龄:xx"的格式将信息打印出来
 *
 *
 */
public class Demo {
    public static void main(String[] args) {
        //体验Consumer接口
        //fun();

        //练习Consumer接口
        //定义数组
        //String [] strArray = {"林青霞,30","张曼玉,35","王祖贤,33"};
        //printInfo(strArray,s -> System.out.print("姓名:"+s.split(",")[0]),s -> System.out.println(",年龄:"+s.split(",")[1]));




        //匿名类对象写法
//        Supplier<String> s = () -> "林青霞2";
//        //System.out.println(s.get());
//        //String s = "林青霞2";
//        Consumer<String> con = new Consumer<String>() {
//            @Override
//            public void accept(String s) {
//                System.out.println(s);
//            }
//        };
//        con.accept(s.get());

        //Lambda 表达式写法
        //生产型接口
        Supplier<String> s2 = () -> "123";
        //String s2 = "阿伟";
        //Lambda表达式
        //Consumer<String> con2 = s -> System.out.println(s);
        //方法引用改进
        Consumer<String> con2 = System.out::println;
        Consumer<String> con3 = s -> System.out.println(new StringBuilder(s).reverse().toString());
        con2.andThen(con3).accept(s2.get());


    }


    //消费数组中的数据
    private static void printInfo(String[] strArray,Consumer<String> con, Consumer<String> con2){
        for (String str : strArray){
            con.andThen(con2).accept(str);
        }
    }

    private static void fun() {
        operatorString("林青霞",s -> System.out.println(s));
        operatorString("张曼玉",System.out::println);
        operatorString("柳岩",s -> System.out.println(new StringBuilder(s).reverse().toString()));
        System.out.println("----------------");
        //消费两次
        operatorString("柳岩",s -> System.out.println(s),s -> System.out.println(new StringBuilder(s).reverse().toString()));
    }


    //定义一个方法,消费一个字符串
    private static void  operatorString(String name, Consumer<String> con){
        con.accept(name);
    }
    //定义一个方法,用不同的方式,消费一个字符串,两次
    private static void  operatorString(String name, Consumer<String> con, Consumer<String> con2){
        con.andThen(con2).accept(name);
    }

}


















